﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Win.CSkin.Test.UserControlExt
{
    public partial class DataGridViewEx : DataGridView
    {
        /// <summary>
        /// 默认的序号列的宽度
        /// </summary>
        int _RowHeadWidth = 41;

        public DataGridViewEx()
        {
            InitializeComponent();
        }

        public DataGridViewEx(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        /// <summary>
        /// 重绘Column、Row
        /// </summary>
        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
            //如果是Column
            if (e.RowIndex == -1)
            {
                drawColumnAndRow(e);
                e.Handled = true;
            }
            else if (e.ColumnIndex < 0 && e.RowIndex >= 0)//如果是Rowheader
            {
                drawColumnAndRow(e);
                e.Handled = true;
            }
        }

        /// <summary>
        /// Column和RowHeader绘制
        /// </summary>
        /// <param name="e"></param>
        void drawColumnAndRow(DataGridViewCellPaintingEventArgs e)
        {
            // 绘制背景色 创建渐变画刷
            using (LinearGradientBrush backbrush =
                new LinearGradientBrush(e.CellBounds,
                    ProfessionalColors.MenuItemPressedGradientBegin,
                    ProfessionalColors.MenuItemPressedGradientMiddle
                    , LinearGradientMode.Vertical))
            {

                Rectangle border = e.CellBounds;
                border.Width -= 1;
                //填充绘制效果 当前区域内绘制画刷效果
                e.Graphics.FillRectangle(backbrush, border);
                //绘制Column、Row的Text信息
                e.PaintContent(e.CellBounds);
                //绘制边框
                ControlPaint.DrawBorder3D(e.Graphics, e.CellBounds, Border3DStyle.Etched);
            }
        }


        /// <summary>
        /// 重绘选中行
        /// </summary>
        protected override void OnRowPrePaint(DataGridViewRowPrePaintEventArgs e)
        {
            //base.OnRowPrePaint(e);

            //是否是选中状态
            if ((e.State & DataGridViewElementStates.Selected) ==
                        DataGridViewElementStates.Selected)
            {
                // 获取当前可视化Column的宽度，也就是选择区域的宽度
                int width = this.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + _RowHeadWidth;

                Rectangle rowBounds = new Rectangle(
                  0, e.RowBounds.Top, width,
                    e.RowBounds.Height);

                // 绘制选中背景色
                using (LinearGradientBrush backbrush =
                    new LinearGradientBrush(rowBounds,
                        Color.GreenYellow,
                        e.InheritedRowStyle.ForeColor, 90.0f))
                {
                    e.Graphics.FillRectangle(backbrush, rowBounds);
                    e.PaintCellsContent(rowBounds);
                    e.Handled = true;
                }

            }
        }

        /// <summary>
        /// 重绘选中行
        /// </summary>
        protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
        {
            //base.OnRowPostPaint(e);
            int width = this.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + _RowHeadWidth;
            Rectangle rowBounds = new Rectangle(
                   0, e.RowBounds.Top, width, e.RowBounds.Height);

            if (this.CurrentCellAddress.Y == e.RowIndex)
            {
                //设置选中边框
                e.DrawFocus(rowBounds, true);
            }
        }

    }
}
